/**
 * @Title: IndexServiceImpl.java
 * @Package com.zll.ccp.service.impl
 * @author zhanglinlu
 * @date 2019年5月17日 下午12:03:07
 */
package com.zll.ccp.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zll.ccp.mapper.IUserMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zll.ccp.mapper.IAnnounceMapper;
import com.zll.ccp.service.IIndexService;

/**
 * @ClassName: IndexServiceImpl
 * @Description: 主页service实现类
 * @author zhanglinlu
 * @date 2019年5月17日 下午12:03:07
 * 
 */
@Service
@Transactional
public class IndexServiceImpl implements IIndexService {
    @Autowired
    private IUserMapper IUserMapper;
    @Autowired
    private IAnnounceMapper announceMapper;

    /* (non-Javadoc)
     * @param yearMonth
     * @return
     * @see com.zll.ccp.service.IIndexService#getUserShareCount(java.lang.String)
     */
    @Override
    public List<Map<String, Object>> getUserShareCount(String yearMonth) {
        List<Map<String, Object>> userShareCounts = IUserMapper.getUserShareCount(yearMonth);
        if (CollectionUtils.isEmpty(userShareCounts)) {
            userShareCounts = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                Map<String, Object> item = new HashMap<>();
                item.put("username", "----");
                item.put("count", "----");
                userShareCounts.add(item);
            }
        } else if (userShareCounts.size() < 5) {
            int rest = 5 - userShareCounts.size();
            for (int i = 0; i < rest; i++) {
                Map<String, Object> item = new HashMap<>();
                item.put("username", "----");
                item.put("count", "----");
                userShareCounts.add(item);
            }
        }
        return userShareCounts;
    }

    /* (non-Javadoc)
     * @return
     * @see com.zll.ccp.service.IIndexService#getAnnouncesTop()
     */
    @Override
    public List<Map<String, Object>> getAnnouncesTop() {
        List<Map<String, Object>> announcesTops = announceMapper.getAnnouncesTop();
        if (CollectionUtils.isEmpty(announcesTops)) {
            announcesTops = new ArrayList<>();
            for (int i = 0; i <= 4; i++) {
                Map<String, Object> item = new HashMap<>();
                item.put("title", "----");
                item.put("createtime", "----");
                announcesTops.add(item);
            }
        } else if (announcesTops.size() < 5) {
            int rest = 5 - announcesTops.size();
            for (int i = 0; i < rest; i++) {
                Map<String, Object> item = new HashMap<>();
                item.put("title", "----");
                item.put("createtime", "----");
                announcesTops.add(item);
            }
        }
        return announcesTops;
    }

}
